package net.gyapp.markim.module.mine.account;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.widget.EditText;
import android.widget.TextView;

import com.easyder.wrapper.base.view.WrapperSwipeActivity;
import com.easyder.wrapper.core.model.BaseVo;

import net.gyapp.markim.ApiConfig;
import net.gyapp.markim.R;
import net.gyapp.markim.module.basic.base.CommonPresenter;
import net.gyapp.markim.module.basic.event.AccountChanged;
import net.gyapp.markim.module.basic.event.AccountIml;
import net.gyapp.markim.utils.RequestParams;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.OnClick;
import me.winds.widget.usage.TitleView;

/**
 * Author:  winds
 * Data:    2018/7/7
 * Version: 1.0
 * Desc:
 */


public class ModifyTextActivity extends WrapperSwipeActivity<CommonPresenter> {

    public final static int SIGN_MODIFY_NICK = 10001;
    public final static int SIGN_MODIFY_REMARK = 10002;

    @BindView(R.id.et_text)
    EditText etText;
    @BindView(R.id.tv_tip)
    TextView tvTip;

    String text;
    String friendId;
    int sign;

    public static Intent getIntent(Context context, String title, String text, String tip, int limit, int sign) {
        return new Intent(context, ModifyTextActivity.class)
                .putExtra("title", title)
                .putExtra("text", text)
                .putExtra("tip", tip)
                .putExtra("limit", limit)
                .putExtra("sign", sign);
    }

    public static Intent getIntent(Context context, String title, String text, String tip, int limit, int sign, String friendId) {
        return new Intent(context, ModifyTextActivity.class)
                .putExtra("title", title)
                .putExtra("text", text)
                .putExtra("tip", tip)
                .putExtra("limit", limit)
                .putExtra("friendId", friendId)
                .putExtra("sign", sign);
    }

    @Override
    protected int getViewLayout() {
        return R.layout.activity_modify_text;
    }

    @Override
    protected void initView(Bundle savedInstanceState, TitleView titleView, Intent intent) {
        titleView.setCenterText(intent.getStringExtra("title"));
        text = intent.getStringExtra("text");
        etText.setText(text);
        tvTip.setText(intent.getStringExtra("tip"));
        int limit = intent.getIntExtra("limit", -1);
        sign = intent.getIntExtra("sign", SIGN_MODIFY_NICK);
        if (sign == SIGN_MODIFY_REMARK) {
            friendId = intent.getStringExtra("friendId");
        }
    }

    @Override
    protected void loadData(Bundle savedInstanceState, Intent intent) {

    }

    @Override
    public void showContentView(String url, BaseVo dataVo) {
        if (url.contains(ApiConfig.API_MODIFY_INFO)) {
            showToast("修改成功");
            EventBus.getDefault().post(new AccountChanged(AccountIml.ACCOUNT_NICK));
            finish();
        }else if(url.contains(ApiConfig.API_SET_REMARK)) {
            showToast("修改成功");
            Intent i = new Intent();
            i.putExtra("remark", etText.getText().toString());
            setResult(RESULT_OK, i);
            finish();
        }
    }


    @OnClick(R.id.btn_confirm)
    public void onViewClicked() {
        String text = etText.getText().toString();
        if (TextUtils.isEmpty(text)) {
            showToast("请输入");
            return;
        }

        if (!TextUtils.isEmpty(this.text)) {
            if (this.text.equals(text)) {
                showToast("请输入要修改的内容");
                return;
            }
        }

        switch (sign) {
            case SIGN_MODIFY_NICK:
                modify(text);
                break;
            case SIGN_MODIFY_REMARK:
                setRemark(text);
                break;
        }
    }

    private void setRemark(String remark) {
        presenter.postData(ApiConfig.API_SET_REMARK, new RequestParams().put("remark", remark).put("friendId", friendId).get(), BaseVo.class);
    }

    private void modify(String text) {
        presenter.postData(ApiConfig.API_MODIFY_INFO, new RequestParams().put("nick", text).get(), BaseVo.class);
    }
}
